package com.qf.j2112.utils;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * @ProjectName: YiZhanXiYi
 * @Package: com.qf.j2112.utils
 * @ClassName: RedisUtilSerializer
 * @Author: 宋晓博
 * @Description: description
 * @Date: 2022/4/20 10:46
 * @Version: 1.0
 */
public class RedisUtilSerializer implements RedisSerializer<Object> {
    //获取序列化的转换器
    Converter<Object, byte[]> serializingConverter = new SerializingConverter();
    //获取反序列化转换器
    Converter<byte[], Object> deserializingConverter = new DeserializingConverter();
    private static final byte[] EMPTY_BYTE_ARRAY  = new byte[0];//声明一个数组常量
    @Override
    public byte[] serialize(Object o) throws SerializationException {

        if(o == null){
            return EMPTY_BYTE_ARRAY;
        }
        return serializingConverter.convert(o);   //指定对value做序列化转换
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if(bytes==null){
            return null;
        }
        return deserializingConverter.convert(bytes);
    }
}
